//
//  MBStoryRecommendCell.m
//  MiBaoTu
//
//  Created by admin on 16/9/26.
//  Copyright © 2016年 com.caesartech15. All rights reserved.
//

#import "MBStoryRecommendCell.h"

@interface MBStoryRecommendCell () {
    UIView              *_bgView;
    UIImageView         *_nickImgView;
    UILabel             *_nickNameLb;
    UILabel             *_contentLb;
}

@end
@implementation MBStoryRecommendCell

- (void)awakeFromNib {
    // Initialization code
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self onCreateUI];
    }
    return self;
}

- (void)onCreateUI
{
    self.backgroundColor = [UIColor clearColor];
    self.backgroundView = nil;
    
    //背景
    _bgView = [[UIView alloc]initWithFrame:CGRectZero];
    _bgView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:_bgView];
    
    //头像
    _nickImgView = [[UIImageView alloc]initWithFrame:CGRectZero];
    _nickImgView.backgroundColor = [UIColor clearColor];
    [_bgView addSubview:_nickImgView];
    
    //标题
    _nickNameLb = [[UILabel alloc]initWithFrame:CGRectZero];
    _nickNameLb.backgroundColor = [UIColor clearColor];
    _nickNameLb.textAlignment = NSTextAlignmentLeft;
    _nickNameLb.textColor = UIColorFromHex(0x333333);
    _nickNameLb.font = [UIFont systemFontOfSize:14.f];
    [_bgView addSubview:_nickNameLb];
    
    //内容
    _contentLb = [[UILabel alloc]initWithFrame:CGRectZero];
    _contentLb.backgroundColor = [UIColor clearColor];
    _contentLb.textAlignment = NSTextAlignmentLeft;
    _contentLb.textColor = UIColorFromHex(0x999999);
    _contentLb.font = [UIFont systemFontOfSize:12.f];
    _contentLb.numberOfLines = (ISIPAD?3:2);
    [_bgView addSubview:_contentLb];
}

- (void)setOrientation:(UIDeviceOrientation)orientation
{
    _orientation = orientation;
    [self setNeedsLayout];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)setDataDic:(NSDictionary *)dataDic
{
    _dataDic = dataDic;
    [self setNeedsLayout];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    //value
    /*
     {
     "ID": 1,
     "UserID": 63,
     "ShowTitle": "1",
     "FilePath": "",
     "Sentiment": "2",
     "ShowType": 2,
     "CoverPath": "",
     "AuditionNum": 0,
     "PraisedNum": 0,
     "FansNum": 0,
     "ShowStatus": 1,
     "CreateTime": 0
     }
     */
    if(!_dataDic || _dataDic.allKeys.count == 0) {
        self.hidden = YES;
        _bgView.hidden = YES;
        return;
    }
    self.hidden = NO;
    _bgView.hidden = NO;
    
    //头像
    NSString *FaceImg = [_dataDic objectForKey:@"FaceImg"];
    if ([FaceImg isKindOfClass:[NSNull class]]) {
        FaceImg = @"";
    }
    if (FaceImg.length > 0) {
        [_nickImgView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",MUSIC_PLAY_Url,FaceImg]] placeholderImage:[UIImage imageNamed:@"xiaobian_bg"]];
    } else {
        _nickImgView.image = [UIImage imageNamed:@"xiaobian_bg"];
    }
    //昵称
    NSString *nick = [_dataDic objectForKey:@"NickName"];
    if ([nick isKindOfClass:[NSNull class]]) {
        nick = @"";
    }
    _nickNameLb.text = nick;
    //介绍
    NSString *Describe = [_dataDic objectForKey:@"Describe"];
    if ([Describe isKindOfClass:[NSNull class]]) {
        Describe = @"";
    }
    _contentLb.text = Describe;
    
    //frame
    _bgView.frame = CGRectMake(0, 0, SCREEN_WIDTH, self.height);
    
    _nickImgView.frame = CGRectMake((ISIPAD?460*PERCEN*(_orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.):25*PERCEN), (_bgView.height-120*PERCEN)/2., 120*PERCEN, 120*PERCEN);
    _nickImgView.layer.cornerRadius = _nickImgView.height/2.;
    _nickImgView.layer.masksToBounds = YES;
    
    _nickNameLb.frame = CGRectMake(_nickImgView.right+20*PERCEN, 20*PERCEN+5, _bgView.width-_nickImgView.right-(ISIPAD?460*PERCEN*(_orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.):45*PERCEN), 20);
    
    float itemTop = (WIDTH_5S ? _bgView.height-40-24*PERCEN: _bgView.height-40-28*PERCEN);
    if (ISIPAD) {
        itemTop = _nickNameLb.bottom-6;
    }
    _contentLb.frame = CGRectMake(_nickNameLb.left,itemTop , _nickNameLb.width, (ISIPAD?60:40));
    float height = [_contentLb.text boundingRectWithSize:CGSizeMake(_contentLb.width, 1000) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:_contentLb.font} context:nil].size.height;
    if (ISIPAD) {
        
    } else {
        if (height <= 21) {
            //只有一行
            _contentLb.top = itemTop+10;
        } else {
            _contentLb.top = itemTop+10;
        }
    }
}

@end
